/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t__te_pkt_is_fragmented.c                          */
/*  NOTE      =                                                    */
/*  DATE      = 2017/02/24 by songbb                               */
/*******************************************************************/

#include <CUnit/Basic.h>

#include "frcomm.h"
#include "frmem.h"

#include <tongeasy/config.h>
#include <tongeasy/te_core.h>
#include <tongeasy/te_packet.h>
#include <apmtest_comm.h>

extern void __te_destroy_packet(L7_te_packet **pp_st_te_packet);
extern int __te_pkt_is_fragmented(L7_te_packet *p_st_te_packet
                                    , char *p_data
                                    , unsigned int ui_len
                                    , unsigned int *p_ui_len_read
                                    , unsigned int *p_ui_len_to_read);

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

/**
 * 正常测试
 */
static void __case1(void)
{
    L7_te_packet *p_st_te_packet = NULL;
    p_st_te_packet = (L7_te_packet *) frmalloc(sizeof(L7_te_packet));
    CU_ASSERT_PTR_NOT_NULL(p_st_te_packet);
    memset(p_st_te_packet, 0x00, sizeof(L7_te_packet));

    CU_ASSERT_EQUAL(te_init_packet(p_st_te_packet), FR_SUCC);

    char *p_bin = (char *) "cf0700000a6fce009325000064656d6f73767200000000000064656d6f636c690000000000000000b2080000355a303230307c30303037303030307c30303434303030307c64656d6f636c69202020202064656d6f636c69202020202064656d6f73767220202020203733202020203020202064656d6f636c6920202020203020202000000000000000000000000063616c6c5f7265715f66756e3120202020202020303032312020302020203020202031302020333020207468697320636c69656e742073796e632064617461";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    unsigned int ui_len_read = 0;       // 当前读了多少个字节
    unsigned int ui_len_to_read = 0;    // 还需要读多少个字节

    CU_ASSERT_EQUAL(__te_pkt_is_fragmented(p_st_te_packet, arr_payload, strlen(p_bin) / 2, &ui_len_read, &ui_len_to_read), APM_TE_PACKET_UNFRAGMENTED);
    CU_ASSERT_EQUAL(ui_len_read, strlen(p_bin) / 2);
    CU_ASSERT_EQUAL(ui_len_to_read, 0);

    __te_destroy_packet(&p_st_te_packet);
    CU_ASSERT_PTR_NULL(p_st_te_packet);
}

/**
 * 正常测试
 */
static void __case2(void)
{
    L7_te_packet *p_st_te_packet = NULL;
    p_st_te_packet = (L7_te_packet *) frmalloc(sizeof(L7_te_packet));
    CU_ASSERT_PTR_NOT_NULL(p_st_te_packet);
    memset(p_st_te_packet, 0x00, sizeof(L7_te_packet));

    CU_ASSERT_EQUAL(te_init_packet(p_st_te_packet), FR_SUCC);

    char *p_bin = (char *) "cf0700000a6fce009325000064656d6f73767200000000000064656d6f636c690000000000000000b2080000355a303230307c30303037303030307c30303434303030307c64656d6f636c69202020202064656d6f636c69202020202064656d6f73767220202020203733202020203020202064656d6f636c6920202020203020202000000000000000000000000063616c6c5f7265715f66756e3120202020202020303032312020302020203020202031302020333020207468697320636c69656e742073796e6320646174";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    unsigned int ui_len_read = 0;       // 当前读了多少个字节
    unsigned int ui_len_to_read = 0;    // 还需要读多少个字节

    CU_ASSERT_EQUAL(__te_pkt_is_fragmented(p_st_te_packet, arr_payload, strlen(p_bin) / 2, &ui_len_read, &ui_len_to_read), APM_TE_PACKET_FRAGMENTED);
    CU_ASSERT_EQUAL(ui_len_read, strlen(p_bin) / 2);
    CU_ASSERT_EQUAL(ui_len_to_read, 1);

    __te_destroy_packet(&p_st_te_packet);
    CU_ASSERT_PTR_NULL(p_st_te_packet);
}

/**
 * 异常测试
 */
static void __case3(void)
{
    L7_te_packet *p_st_te_packet = NULL;
    p_st_te_packet = (L7_te_packet *) frmalloc(sizeof(L7_te_packet));
    CU_ASSERT_PTR_NOT_NULL(p_st_te_packet);
    memset(p_st_te_packet, 0x00, sizeof(L7_te_packet));

    CU_ASSERT_EQUAL(te_init_packet(p_st_te_packet), FR_SUCC);

    char *p_bin = (char *) "ca0700000a6fce009325000064656d6f73767200000000000064656d6f636c690000000000000000b2080000355a303230307c30303037303030307c30303434303030307c64656d6f636c69202020202064656d6f636c69202020202064656d6f73767220202020203733202020203020202064656d6f636c6920202020203020202000000000000000000000000063616c6c5f7265715f66756e3120202020202020303032312020302020203020202031302020333020207468697320636c69656e742073796e632064617461";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    unsigned int ui_len_read = 0;       // 当前读了多少个字节
    unsigned int ui_len_to_read = 0;    // 还需要读多少个字节

    CU_ASSERT_EQUAL(__te_pkt_is_fragmented(p_st_te_packet, arr_payload, strlen(p_bin) / 2, &ui_len_read, &ui_len_to_read), APM_TE_PACKET_ERROR);

    __te_destroy_packet(&p_st_te_packet);
    CU_ASSERT_PTR_NULL(p_st_te_packet);
}

int add_suite__te_pkt_is_fragmented(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" __te_pkt_is_fragmented()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL ||
        CU_add_test(pSuite, "case2", __case2) == NULL ||
        CU_add_test(pSuite, "case3", __case3) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
